package org.xdq.springcloud.demo20191222.zuul;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.context.annotation.Bean;
import org.springframework.session.FlushMode;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
import org.springframework.session.web.http.CookieSerializer;
import org.springframework.session.web.http.DefaultCookieSerializer;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.xdq.base.Constants;

@SpringBootApplication
@EnableZuulProxy
@EnableRedisHttpSession(flushMode = FlushMode.IMMEDIATE)
public class App {

	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}
	
	
	@Bean
    public FilterRegistrationBean<CorsFilter> corsFilter() {
        
    	final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        
    	final CorsConfiguration config = new CorsConfiguration();
        // 允许cookies跨域
        config.setAllowCredentials(true);
        
        // 允许向该服务器提交请求的URI，*表示全部允许，在SpringMVC中，如果设成*，会自动转成当前请求头中的Origin
        config.addAllowedOrigin("*");
        
        // 允许访问的头信息,*表示全部
        config.addAllowedHeader("*");
        
        // 预检请求（options请求）的缓存时间（秒），即在这个时间段里，对于相同的跨域请求不会再预检了
        config.setMaxAge(1L);
        
        // 允许提交请求的方法，*表示全部允许
        config.addAllowedMethod("*");
        /*
        config.addAllowedMethod("OPTIONS");
        config.addAllowedMethod("HEAD");
        config.addAllowedMethod("GET");
        config.addAllowedMethod("PUT");
        config.addAllowedMethod("POST");
        config.addAllowedMethod("DELETE");
        config.addAllowedMethod("PATCH");
        */
        
        //设置允许跨域的路径
        source.registerCorsConfiguration("/**", config);

        FilterRegistrationBean<CorsFilter> bean = new FilterRegistrationBean<CorsFilter>(new CorsFilter(source));
        
        // 设过滤器的优先级
        bean.setOrder(0);

        return bean;
    }
	
	//注册禁用浏览器缓存过滤器
    @Bean
	public FilterRegistrationBean<Filter> noBrowerCacheFilter() {
		
    	FilterRegistrationBean<Filter> bean = new FilterRegistrationBean<Filter>(new Filter() {

			public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
					throws IOException, ServletException {
				
				HttpServletResponse resp = (HttpServletResponse) response;
				resp.addHeader("Pragma", "no-cache");
				resp.setHeader("Cache-Control","no-cache,no-store,must-revalidate");
				resp.addHeader("Cache-Control","pre-check=0, post-check=0");
				resp.setDateHeader("Expires", 0);
				
				chain.doFilter(request, response);
				
			}


			
			
		});
		
		bean.addUrlPatterns("/*");
		bean.setOrder(1);
		
		return bean;
		
	}


//	
//	@Value("${server.servlet.session.cookie.path}")
//	private String path;
//
//	@Value("${server.servlet.session.cookie.domain}")
//	private String domain;
//
//	@Value("${server.servlet.session.cookie.http-only}")
//	private boolean httpOnly;
//
//	@Bean
//	public CookieSerializer cookieSerializer() {
//		DefaultCookieSerializer cookieSerializer = new DefaultCookieSerializer();
//		cookieSerializer.setCookiePath(path);
//		cookieSerializer.setDomainName(domain);
//		cookieSerializer.setUseHttpOnlyCookie(httpOnly);
//		cookieSerializer.setSameSite(null);
//		return cookieSerializer;
//	}

//	@Bean
//	public WebMvcConfigurer webMvcConfigurer() {
//		
//		return new WebMvcConfigurer() {
//			@Override
//			public void addCorsMappings(CorsRegistry registry) {
//				
//				registry.addMapping("/**")// 允许任意地址接受跨域请求
//						.allowCredentials(true)// 允许跨域使用session认证
//						.allowedMethods("*");// 允许任何请求方式跨域
//			}
//	
//		};
//	}
	
	
	


}
